package DMSXL._2024.BinaryTrees;

public class sumOfLeftLeaves {
    public static void main(String[] args) {
        Integer[] nums = {3,9,20,null,null,15,7};
        TreeNode root = CreateTree.createBinaryTree(nums,0);
        System.out.println(new sumOfLeftLeaves().sumOfLeftLeaves(root));
    }
    int result = 0;
    public int sumOfLeftLeaves(TreeNode root) {
        if(root == null){
            return 0;
        }
        preorder(root,false);
        return result;
    }
    public void preorder(TreeNode root, boolean isleft){
        if(root == null){
            return;
        }
        if(root.left != null){
            preorder(root.left,true);
        }
        if(root.right != null){
            preorder(root.right,false);
        }
        if(root.left == null && root.right == null && isleft == true){
            result += root.val;
        }
    }
}
